package pri.zey.custom.tools.image;/**
 * Created by Administrator on 2016/3/16.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class ViewHolder {

    private int mPosition;
    private SparseArray<View> mViews;
    private View mConvertView;

    public ViewHolder(int position, View convertView, ViewGroup parent, Context mContext, int layoutId){
        mViews = new SparseArray<View>();
        this.mPosition = position;
        this.mConvertView = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
        this.mConvertView.setTag(this);
    }

    /**
     *   ViewHolder holder = null;
         if(convertView == null){
             holder = new ViewHolder();
             convertView.setTag(holder);
         }else{
             holder = (ViewHolder) convertView.getTag();
         }
     * 与上面代码同理
     * @param position
     * @param convertView
     * @param parent
     * @param mContext
     * @param layoutId
     * @return
     */
    public static ViewHolder get(int position, View convertView, ViewGroup parent, Context mContext, int layoutId){
        if(convertView == null){
            return new ViewHolder(position, convertView, parent, mContext, layoutId);
        }else{
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.mPosition = position;
            return holder;
        }
    }

    /**
     * 获取显示Item ConvertView
     * @return
     */
    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 根据viewId获取控件
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View>T getView(int viewId){
        View view = mViews.get(viewId);
        if(view == null){
            view = getConvertView().findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T)view;
    }

    /**
     * 设置文本
     * 返回值为void 时候只能使用单个holder.setText() ，如果返回值为ViewHolder 时候可以holder.setText().setText()....
     */
    public ViewHolder setText(int viewId, String TextStr){
        TextView view = getView(viewId);
        view.setText(TextStr);
        return this;
    }

    /**
     * 设置Resource图片
     */
    public ViewHolder setImageResource(int viewId, int resourceId){
        ImageView imageView = getView(viewId);
        imageView.setImageResource(resourceId);
        return this;
    }

    /**
     * 设置Bitmap图片
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap){
        ImageView imageView = getView(viewId);
        imageView.setImageBitmap(bitmap);
        return this;
    }

    /**
     * 设置Url图片
     */
    public ViewHolder setImageBitmap(int viewId, String url){
        ImageView imageView = getView(viewId);
//        imageView.loadeImageView(url);

        return this;
    }
}
